package com.dyh.quickdemozwei.util;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yhdong
 * @Date: 2021/9/6 19:53
 */
public class FileUtil {

    public static String readFile(String path){
        File file = new File(path);
        StringBuffer result = new StringBuffer();

        try (FileInputStream fis = new FileInputStream(file);
             InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
             BufferedReader br = new BufferedReader(isr)
        ){
            String line;
            while((line=br.readLine()) != null){
                result.append(line).append("\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    public static List<String> readLine(String path){
        File file = new File(path);


        List<String> lines = new ArrayList<>();
        try (FileInputStream fis = new FileInputStream(file);
             InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
             BufferedReader br = new BufferedReader(isr)
        ){
            String line;
            while((line=br.readLine()) != null){
                lines.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lines;
    }

    public static void writeFile(String path,String content){
        File file = new File(path);

        try (FileOutputStream fos = new FileOutputStream(file, false);
             OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
             BufferedWriter bw = new BufferedWriter(osw)
        ){
            bw.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
